package com.glela.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.common.constant.CommonConstants;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestVersionModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.model.response.ResponseVersion;
import com.glela.platform.service.VersionService;
import com.glela.platform.util.ParameterUtils;

@Controller
@RequestMapping(value = "version", produces = "application/json;charset=UTF-8")
public class VersionController extends BaseController {
	@Resource
	private VersionService versionService;

	//	@Autowired
	//	VersionMapper versionMapper;
	//
	//	@RequestMapping(value = "/testgetVersionInfo", method = RequestMethod.POST, consumes = "application/json")
	//	public ResponseEntity<?> testgetVersionInfo(@RequestBody RequestVersionModel reVersion) {
	//		try {
	//			return new ResponseEntity<>(new ResponseResultModel<>("1111111111111111111"), HttpStatus.OK);
	//		} catch (Exception e) {
	//			getLOGGER().error("app版本更新异常:", e);
	//			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
	//		}
	//	}
	//
	//	@RequestMapping(value = "/testgetVersionInfo2", method = RequestMethod.POST, consumes = "application/json")
	//	public ResponseEntity<?> testgetVersionInfo2(@RequestBody RequestVersionModel reVersion) {
	//		try {
	//			RequestVersionModel reVersions = new RequestVersionModel();
	//			reVersion.setType(1);
	//			reVersion.setInterfaceVersion("v2.0");
	//			ResponseVersion resVers = versionMapper.getNewVersion(reVersions);
	//
	//			return new ResponseEntity<>(new ResponseResultModel<>("2222222222222"), HttpStatus.OK);
	//		} catch (Exception e) {
	//			getLOGGER().error("app版本更新异常:", e);
	//			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
	//		}
	//	}

	@RequestMapping(value = "/getVersionInfo", method = RequestMethod.POST)
	public ResponseEntity<?> getVersionInfo(@RequestBody RequestVersionModel reVersion) {
		try {
			if (ParameterUtils.validParams(reVersion.getType(), reVersion.getVersionType())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			reVersion.setInterfaceVersion("v1.0");
			ResponseVersion resion = versionService.getVersionInfo(reVersion);

			if (resion != null) {
				return new ResponseEntity<>(new ResponseResultModel<ResponseVersion>(resion), HttpStatus.OK);
			} else {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20168), HttpStatus.OK);
			}
		} catch (Exception e) {
			getLOGGER().error("app版本更新异常:", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	@RequestMapping(value = "/getVersionInfov2", method = RequestMethod.POST)
	public ResponseEntity<?> getVersionInfov2(HttpServletRequest request, @RequestBody RequestVersionModel reVersion) {
		String rslt = null;
		String tempKey = null;
		try {
			String phoneType = request.getHeader("phoneType");
			String appSystemVersion = request.getHeader("appSystemVersion");

			if (ParameterUtils.validParams(phoneType, appSystemVersion)) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			tempKey = getTempCacheKey(phoneType, appSystemVersion);
			rslt = queryCacheResult(tempKey);
			if (StringUtils.isNotEmpty(rslt)) {
				return toRespEntity(rslt);
			}

			phoneType = phoneType.toLowerCase();
			RequestVersionModel req = new RequestVersionModel();
			req.setVersionType("app");
			req.setVersionCode(appSystemVersion);
			req.setInterfaceVersion("v2.0");
			if (CommonConstants.PHONE_CLIENT_IOS.equals(phoneType)) {
				req.setType(2);
			} else if (CommonConstants.PHONE_CLIENT_ANDROID.equals(phoneType)) {
				req.setType(1);
			}
			ResponseVersion rep = versionService.getVersionInfov2(req);
//			if (req.getType() == 2 && "5.3.0".equals(req.getVersionCode()) && rep.getForcedUpdate() == 0 && rep.getSelectUpdate() == 1) {
//				//new ResponseResultModel<Object>(ErrorEnum.C99999,rep)
//				rslt = toErrorModelJSON(ErrorEnum.C99999);
//			} else {
//				rslt = toResultModelJSON(rep);
//			}
			rslt = toResultModelJSON(rep);
		} catch (Exception e) {
			getLOGGER().error("app版本更新异常:", e);
			rslt = toErrorModelJSON(ErrorEnum.C20000);
		}
		return toRespEntity(toCacheResult(tempKey, rslt, 60 * 60));
	}

}
